
(function() {
  var mosUnsavedChanges;

  mosUnsavedChanges = angular.module('unsavedChanges', []);

  mosUnsavedChanges.service('unsavedChanges', [
    '$rootScope', '$window', '$route', function($rootScope, $window, $route) {
      var MESSAGE, fnHash, fnLocationListener;
      MESSAGE = "Are you sure you don't want to save changes?";
      fnLocationListener = null;
      this.fnListen = function($scope, mListenableObject) {
        var sInitialHash,
          _this = this;
        if ($scope.$id !== $route.current.scope.$id) {
          return;
        }
        this.fnRemoveListener();
        sInitialHash = fnHash(mListenableObject);
        fnLocationListener = $rootScope.$on('$locationChangeStart', function(oEvent) {
          var sFinalHash;
          sFinalHash = fnHash(mListenableObject);
          if (sFinalHash !== sInitialHash && !confirm(MESSAGE)) {
            return oEvent.preventDefault();
          } else {
            return _this.fnRemoveListener();
          }
        });
        $window.onbeforeunload = function(evt) {
          var sFinalHash;
          sFinalHash = fnHash(mListenableObject);
          if (sFinalHash !== sInitialHash) {
            if (typeof evt === "undefined") {
              evt = window.event;
            }
            if (evt != null) {
              evt.returnValue = MESSAGE;
            }
            return MESSAGE;
          }
        };
        return $scope.$on('$destroy', function() {
          return _this.fnRemoveListener();
        });
      };
      this.fnRemoveListener = function() {
        if (fnLocationListener != null) {
          fnLocationListener();
        }
        return $window.onbeforeunload = null;
      };
      fnHash = function(something) {
        return angular.toJson(something);
      };
    }
  ]);

}).call(this);
